package org.dromara.tearoom.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.bo.TerUserBo;
import org.dromara.tearoom.domain.bo.TerUserCountTotalBo;
import org.dromara.tearoom.domain.vo.TerUserDayVo;
import org.dromara.tearoom.domain.vo.TerUserVo;
import org.dromara.tearoom.service.ITerUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 * 前端访问路由地址为:/tearoom/user
 *
 * @author xch
 * @date 2025-03-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/teruser")
public class TerUserController extends BaseController {

    private final ITerUserService terUserService;

    @SaCheckPermission("tearoom:order:query")
    @GetMapping("/queryUserCount")
    public R<TerUserDayVo> queryUserCount(@ModelAttribute TerUserCountTotalBo tbo) {
        return R.ok(terUserService.queryUserCount(tbo));
    }


    /**
     * 登录
     */
    @Log(title = "微信回调", businessType = BusinessType.GRANT)
    @GetMapping("/callback/{code}")
    public TerUserVo login(@NotNull(message = "code不能为空") @PathVariable("code") String code) {
        return terUserService.login(code);
    }

    /**
     * 查询用户管理列表
     */
    @SaCheckPermission("tearoom:user:list")
    @GetMapping("/list")
    public TableDataInfo<TerUserVo> list(TerUserBo bo, PageQuery pageQuery) {
        return terUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户管理列表
     */
    @SaCheckPermission("tearoom:user:export")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerUserBo bo, HttpServletResponse response) {
        List<TerUserVo> list = terUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户管理", TerUserVo.class, response);
    }

    /**
     * 获取用户管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:user:query")
    @GetMapping("/{id}")
    public R<TerUserVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable("id") Long id) {
        return R.ok(terUserService.queryById(id));
    }

    /**
     * 新增用户管理
     */
    @SaCheckPermission("tearoom:user:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerUserBo bo) {
        return toAjax(terUserService.insertByBo(bo));
    }

    /**
     * 修改用户管理
     */
    @SaCheckPermission("tearoom:user:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerUserBo bo) {
        return toAjax(terUserService.updateByBo(bo));
    }

    /**
     * 删除用户管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:user:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terUserService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 修改用户头像昵称
     */
    @RepeatSubmit()
    @PutMapping("editInfo")
    public R<Void> editInfo(@Validated(EditGroup.class) @RequestBody TerUserBo bo) {
        return toAjax(terUserService.updateByBo(bo));
    }


    /**
     * 获取用户详细信息
     *
     */
    @GetMapping("/getUserInfo")
    public R<TerUserVo> getUserInfo() {
        TerUserVo terUserVo=terUserService.queryById(LoginHelper.getAppLoginUser().getId());
        if(ObjectUtil.isEmpty(terUserVo)){
            throw new ServiceException("用户不存在");
        }
        if(StrUtil.isBlank(terUserVo.getMobile())){
            throw new ServiceException("用户手机号不存在",100);
        }
        terUserVo.setMobile(DesensitizedUtil.mobilePhone(terUserVo.getMobile()));
        return R.ok(terUserVo);
    }

}
